<template>
  <nav class="navbar navbar-light">
    <div class="container-fluid">
      <a class="navbar-brand">专业百科--管理员登陆</a>
    </div>
  </nav>

  <div class="big-contain row justify-content-md-center" key="bigContainLogin">
    <div class="bform col-3">
      <div class="mb-3">
        <!-- <input type="email" placeholder="用户名" v-model="form.username" /> -->
        <label for="username" class="form-label">用户名</label>
        <input
          v-model="form.adminname"
          type="text"
          class="form-control"
          id="username"
        />
      </div>
      <div class="mb-3">
        <!-- <input type="password" placeholder="密码" v-model="form.userpwd" /> -->
        <label for="password" class="form-label">密码</label>
        <input
          v-model="form.adminpwd"
          type="password"
          class="form-control"
          id="password"
        />
      </div>

      <button class="bbutton btn btn-primary" @click="admin_login">登录</button>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import { useStore } from "vuex";
import router from "@/router/index";

export default {
  name: "AdminLogin",
  components: {},
  data() {
    return {
      form: {
        adminname: "",
        adminpwd: "",
      },
    };
  },
  methods: {
    // toLogin() {
    //   this.$router.push("/admin");
    // },
    admin_login() {
      const self = this;

      if (self.form.adminname != "" && self.form.adminpwd != "") {
        console.log(self.form.adminname);
        console.log(self.form.adminpwd);

        self
          .axios({
            method: "post",
            url: "admin/login",
            data: {
              id: self.form.adminname,
              password: self.form.adminpwd,
            },
          })
          .then((res) => {
            console.log("res", res);
            switch (res.code) {
              case "0":
                sessionStorage.setItem("admin", JSON.stringify(res.data)); // 缓存用户信息
                this.$router.push("/admin");
                alert("登陆成功！");
                break;
              case "-1":
                this.nameError = true;
                break;
              case 1:
                this.passwordError = true;
                break;
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        alert("填写不能为空！");
      }
    },
  },
};
</script>

<style scoped>
button {
  width: 100%;
}

.error-message {
  color: red;
}
</style>
